package com.lzj.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzj.admin.mapper.AreaMapper;
import com.lzj.admin.pojo.Area;
import com.lzj.admin.pojo.Town;
import com.lzj.admin.mapper.TownMapper;
import com.lzj.admin.query.TownQuery;
import com.lzj.admin.service.ITownService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzj.admin.utils.AssertUtil;
import com.lzj.admin.utils.PageResultUtil;
import com.lzj.admin.utils.PinYinUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 乡镇信息表 服务实现类
 * </p>
 *
 * @author luoyoumou
 * @since 2023-01-13
 */
@Service
public class TownServiceImpl extends ServiceImpl<TownMapper, Town> implements ITownService {

    @Resource
    AreaMapper areaMapper;

    @Override
    public Map<String, Object> townList(TownQuery townQuery) {
        IPage<Town> page = new Page<Town>(townQuery.getPage(),townQuery.getLimit());
        page = this.baseMapper.queryTownByParams(page,townQuery);
        return PageResultUtil.getResult(page.getTotal(),page.getRecords());
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void saveTown(Town town) {
        AssertUtil.isTrue(StringUtils.isBlank(town.getName().trim()),"乡镇名不能为空！");
        AssertUtil.isTrue(null != this.findTownByName(town.getName().trim()),"乡镇名已存在！");
        town.setName(town.getName().trim());
        town.setIsDel(0);
        town.setFpyName(PinYinUtil.getFirstSpell(town.getName()));
        AssertUtil.isTrue(!(this.save(town)),"乡镇信息添加失败！");
    }

    /**
     * 查看是否存在 指定名称 的乡镇
     * @param name
     * @return
     */
    private Town findTownByName(String name) {
        return this.getOne(new QueryWrapper<Town>().eq("name",name));
    }

    /**
     * 排除指定乡镇Id, 查看是否存在 指定名称 的乡镇
     * @param name
     * @param id
     * @return
     */
    private Town findTownByName(String name, Integer id) {
        return this.getOne(new QueryWrapper<Town>().eq("name",name.trim()).notIn("id",id));
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateTown(Town town) {
        AssertUtil.isTrue(null == this.getById(town.getId()),"请选择乡镇记录！");
        AssertUtil.isTrue(null != this.findTownByName(town.getName().trim(),town.getId()),"乡镇名已存在！");
        town.setName(town.getName().trim());
        town.setFpyName(PinYinUtil.getFirstSpell(town.getName()));
        AssertUtil.isTrue(!(this.updateById(town)),"乡镇信息更新失败！");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteTown(Integer id) {
        Town tempTown = this.getById(id);
        AssertUtil.isTrue(null == tempTown,"待删除的乡镇记录不存在！");
        QueryWrapper<Area> areaQueryWrapper = new QueryWrapper<Area>().eq("town_id",id).last("limit 1");
        List<Area> area = areaMapper.selectList(areaQueryWrapper);
        AssertUtil.isTrue(!(area.isEmpty()),"该乡镇已存在村信息，不能被删除！");
        tempTown.setIsDel(1);
        AssertUtil.isTrue(!(this.updateById(tempTown)),"乡镇信息删除失败！");
    }
}
